﻿using System;
namespace Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="toimiku_dokumendidVastus", Namespace="http://producers.arireg.xtee.riik.ee/producer/arireg", Target=typeof(ToimikuDokumendidVastus))]
    public class ToimikuDokumendidVastusSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected IToimikuDokumendidVastus clazz;
        
        public ToimikuDokumendidVastusSerializer(IToimikuDokumendidVastus clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("dokumendid");
            if (clazz.Dokumendid!=null) {
                Writer.WriteTypeAttribute("http://schemas.xmlsoap.org/soap/encoding/", "Array");
                Writer.WriteArrayTypeAttribute("toimiku_dokumendid_dokument", "http://producers.arireg.xtee.riik.ee/producer/arireg", clazz.Dokumendid.Count);
                if (((clazz.Dokumendid == null) 
                            || (clazz.Dokumendid.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.Dokumendid != null) 
                            && (x < clazz.Dokumendid.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.Dokumendid[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaDokumendid = this.GetMetaData(clazz.Dokumendid[x]);
                        Writer.WriteTypeAttribute(metaDokumendid.TypeQualifiedName.Namespace, metaDokumendid.TypeQualifiedName.Name);
                        metaDokumendid.NewSerializer(clazz.Dokumendid[x]).WriteXml(Writer);
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Dokumendid");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
        START:
            if ((Reader.LocalName == "dokumendid")) {
                goto dokumendid;
            }
            else {
                return;
            }
        dokumendid:
            if (Reader.IsElementNil()) {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>toimiku_dokumendidVastus>dokumendid");
            }
            clazz.Dokumendid = new System.Collections.Generic.List<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IToimikuDokumendidDokument>();
            if ((Reader.IsEmptyElement == false)) {
                Reader.ReadStartElement("dokumendid");
                for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                            && (Reader.LocalName == "item")); ) {
                    if ((Reader.IsElementNil() == false)) {
                        global::Xtee.Core.SerializationMetadata metaDokumendid = this.GetMetaData<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IToimikuDokumendidDokument>(Reader, "toimiku_dokumendid_dokument", "http://producers.arireg.xtee.riik.ee/producer/arireg");
                        global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IToimikuDokumendidDokument item = metaDokumendid.NewEntity<global::Producers.Arireg.Xtee.Riik.Ee.Producer.Arireg.IToimikuDokumendidDokument>();
                        clazz.Dokumendid.Add(item);
                        if ((Reader.IsEmptyElement == false)) {
                            Reader.ReadStartElement("item");
                            metaDokumendid.NewSerializer(item).ReadXml(Reader);
                            Reader.ReadEndElement();
                        }
                        else {
                            Reader.Skip();
                        }
                    }
                    else {
                        clazz.Dokumendid.Add(null);
                        Reader.Skip();
                    }
                }
                if ((clazz.Dokumendid.Count < 1m)) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                Reader.ReadEndElement();
            }
            else {
                Reader.Skip();
            }
            goto START;
        }
    }
}
